package com.atguigu.flinkSql2;




import com.atguigu.been.WaterSensor;
import org.apache.calcite.rel.core.Calc;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.AggregateFunction;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;


/**
 * @author wky
 * @create 2021-07-22-10:40
 */

// 聚合函数
public class Flink08_UDAF_AggregateFunction {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);
        //将默认时区从格林威治时区改为东八区
        Configuration configuration = tableEnvironment.getConfig().getConfiguration();
        configuration.setString("table.local-time-zone", "GMT");
        //2.读取文件得到DataStream
        DataStreamSource<WaterSensor> waterSensorDataStreamSource = env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                new WaterSensor("sensor_1", 2000L, 20),
                new WaterSensor("sensor_2", 3000L, 30),
                new WaterSensor("sensor_1", 4000L, 40),
                new WaterSensor("sensor_1", 5000L, 50),
                new WaterSensor("sensor_2", 6000L, 60));

        //3.将流转换为动态表
        Table table = tableEnvironment.fromDataStream(waterSensorDataStreamSource);


//        //不注册函数直接使用 只能在tableApi中使用
//        table
//                .groupBy($("id"))
//                .select($("id"), call(MyAvg.class,$("vc"))).execute().print();
//        注册函数 再使用 可以在sql语句中
        tableEnvironment.createTemporarySystemFunction("Myavg",MyAvg.class);
//        table
//                .groupBy($("id"))
//                .select($("id"), call("Myavg",$("vc"))).execute().print();


        //todo
        tableEnvironment.executeSql("select id , Myavg(vc) from " +table+" group by id ").print();

    }
    //自定义累加器
    public static class MyAvgAccumulator {
        public long sum = 0;
        public int count = 0;
    }

    //自定义UDAF函数,求每个WaterSensor中VC的平均值
    public static class MyAvg extends AggregateFunction<Double,MyAvgAccumulator> {

        //创建累加器
        @Override
        public MyAvgAccumulator createAccumulator() {
            return new MyAvgAccumulator();
        }
        //累加操作
        public void accumulate(MyAvgAccumulator acc,Integer value){
            acc.count+=1;
            acc.sum+=value;
        }
        //返回结果
        @Override
        public Double getValue(MyAvgAccumulator accumulator) {
            return accumulator.sum*1D/accumulator.count;
        }
    }
}
